package com.manager.system.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.System_Permission;
import com.manager.model.System_Role;
import com.manager.model.System_Role_Permission;
import com.manager.system.service.RoleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RoleServiceImpl extends SuperDao<Object> implements RoleService {

    @Override
    @Transactional
    public PageVO getRoleList(System_Role model) {
        // TODO Auto-generated method stub
        String sql = "from System_Role t  ";
        List<Object> params = new ArrayList<>();

        if(StringUtils.isNotEmpty(model.getName())){
            sql += " where t.name like ?1";
            params.add("%"+model.getName()+"%");
        }
        List<System_Role> list = this.getList(sql, params.toArray(), model.getStart(), model.getLimit());
        int sum = this.getListCount( "select count(t.id) " + sql, params.toArray(), "");
        int count = (int) Math.ceil((Double.valueOf(sum)/Double.valueOf(model.getLimit())));

        return PageVO.create(model.getStart(),count, ObjectUtils.getJSONArray(System_Role.config,list));
    }

    @Override
    @Transactional
    public void addRole(System_Role model) {
        // TODO Auto-generated method stub
        String menuIds = model.getString1();
        List<String> ids = new ArrayList<String>();
        if(menuIds.contains(",")){
            ids.addAll(Arrays.asList(menuIds.split(",")));
        }else{
            ids.add(menuIds);
        }
        this.addT(model);
        for(String id:ids){
            System_Permission permission = (System_Permission) this.getT(new System_Permission(), id);
            if(permission == null){
                continue;
            }
            System_Role_Permission role_Permission = new System_Role_Permission();
            role_Permission.setSys_role(model);
            role_Permission.setSys_perm(permission);
            this.addT(role_Permission);
        }
    }

    @Override
    @Transactional
    public void updateRole(System_Role model) {
        // TODO Auto-generated method stub
        System_Role role = (System_Role) this.getT(new System_Role(), model.getId());
        String menuIds = model.getString1();
        List<String> ids = new ArrayList<String>();
        if(menuIds.contains(",")){
            ids.addAll(Arrays.asList(menuIds.split(",")));
        }else{
            ids.add(menuIds);
        }
        BeanUtils.copyProperties(model, role, new String[]{"listur","listrp","id","debtManagerList"});
        this.batchDelete(role.getListrp());
        role.getListrp().clear();
        this.updateT(role);
        for(String id:ids){
            System_Permission permission = (System_Permission) this.getT(new System_Permission(), id);
            if(permission == null){
                continue;
            }
            System_Role_Permission role_Permission = new System_Role_Permission();
            role_Permission.setSys_role(role);
            role_Permission.setSys_perm(permission);
            this.addT(role_Permission);
        }
    }

    @Override
    @Transactional
    public JSONArray getMenuTree(System_Role model) {
        // TODO Auto-generated method stub
        String sql = "from System_Permission t ";
        List<System_Permission> list = this.getList(sql, 0, 0, " order by t.sort ");
        JSONArray jsonArray = ObjectUtils.getJSONArray(System_Permission.config, list);
        sql = "from System_Role_Permission t where t.sys_role.id = ?1 ";
        List<System_Role_Permission> list2 = this.getList(sql, new Object[]{model.getId()}, 0, 0);

        for(int i = 0;i < jsonArray.size();i++){
            JSONObject json = jsonArray.getJSONObject(i);
            for(System_Role_Permission role_Permission:list2){
                if(json.getString("id").equals(role_Permission.getSys_perm().getId())){
                    json.put("checked", true);
                }
            }

        }

        return jsonArray;
    }

    @Override
    @Transactional
    public JSONArray getUserMenuByRoleId(String roleId) {
        // TODO Auto-generated method stub
        String sql = "select t from System_Permission t left join t.listrp tr where tr.sys_role.id = ?1 ";
        List<System_Permission> list = this.getList(sql, new Object[]{roleId}, 0, 0);

        return ObjectUtils.getJSONArray(System_Permission.config, list);
    }

    @Override
    @Transactional
    public JSONArray getRoleTreeData() {
        // TODO Auto-generated method stub
        String sql = "from System_Role t where t.status = '1' ";
        List<System_Role> list = this.getList(sql, 0, 0, "");

        return ObjectUtils.getJSONArray(System_Role.config, list);
    }

    @Override
    @Transactional
    public JSONArray getUserMenuByUserId(String userId) {
        // TODO Auto-generated method stub
        String sql = "select t from System_Permission t left join t.listrp  tr left join tr.sys_role trr left join trr.listur tru  where tru.sys_user.id = ?0 ";
        List<System_Permission> list = this.getList(sql, new Object[]{userId}, 0, 0);

        return ObjectUtils.getJSONArray(System_Permission.config, list);
    }

    @Override
    @Transactional
    public void deleteRole(System_Role model) {
        // TODO Auto-generated method stub
        System_Role role = (System_Role) this.getT(new System_Role(), model.getId());
        this.delete(role);
    }


    @Override
    @Transactional
    public List<System_Role> getSystemRoleList() {
        String sql = " select t from System_Role t  ";
        List<System_Role> list = this.getList(sql,0,0,"");
        return list;
    }

    @Override
    @Transactional
    public List<System_Role> getUserRoleList(String userId) {
        String sql = " select t from  System_Role t left join t.listur tl where tl.sys_user.id = ?0 ";
        List<System_Role> list = this.getList(sql,new Object[]{userId},0,0,"");
        return list;
    }
}
